package com.oa.action;

import java.io.InputStream;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.oa.entity.Application;
import com.oa.entity.ApproveInfo;
import com.oa.entity.Template;
import com.oa.service.ApplicationService;
import com.oa.service.TemplateService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
 * @author FengMengHuo
 * 申请 Action
 */
@Controller
@Scope("prototype")
public class ApplicationAction extends ActionSupport implements ModelDriven<Application>{
	private static final long serialVersionUID = 1L;
	private Application application = new Application();
	// 注入 申请服务类
	@Resource(name="applicationService")
	private ApplicationService applicationService ;
	// 注入 模板服务类
	@Resource(name="templateService")
	private TemplateService templateService;
	//申请列表
	public String list(){
		List<Template> templateList =  templateService.findTemplateList();
		ActionContext.getContext().put("templateList", templateList);
		return "list";
	}
	//查看申请模板
	public String look(){
		return "lookTemplate";
	}
	//提交申请模板
	public String addPage(){
		applicationService.addApplication(application);
		return "toList";
	}
	// 我的申请页面
	public String myApplicationList(){
		List<Template> templateList =  templateService.findTemplateList();
		ActionContext.getContext().put("templateList", templateList);
		
		List<Application> applicationList = applicationService.findApplicationList(application);
		ActionContext.getContext().put("applicationList", applicationList);
		return "myApplicationList";
	}
	// 待我审批
	public String myTaskList(){
		List<Application> variableList = applicationService.findApplicationVariable();
		ActionContext.getContext().put("variableList", variableList);
		return "myTaskList";
	}
	// 处理审批
	public String flowApprove(){
		Collection<String> collection = applicationService.getOutComeTransition(application);
		ActionContext.getContext().put("collection", collection);
		return "flowApprove";
	}
	//下载申请模板
	public String download(){
		InputStream inputStream = applicationService.findInputStreamByPath(application);
		//放置到模型驱动中
		application.setInputStream(inputStream);
		return "download";
	}
	// 审批处理
	public String approve(){
		applicationService.approveInfo(application);
		return "approve";
	}
	//查看审核历史
	public String flowApprovedHistory(){
		List<ApproveInfo> list = applicationService.findApproveInfoListByApplicationID(application);
		ActionContext.getContext().put("approveList", list);
		return "flowApprovedHistory";
	}
	// 返回模型层
	public Application getModel() {
		return application;
	}
}
